package com.chinawayltd.ews.sample.service.impl;

import com.chinawayltd.ews.sample.config.InterfaceConfig;
import com.chinawayltd.ews.sample.manager.openApi.OpenApiManager;
import com.chinawayltd.ews.sample.mapper.TVehicleCustomMapper;
import com.chinawayltd.ews.sample.mapper.TVehicleMapper;
import com.chinawayltd.ews.sample.model.TVehicle;
import com.chinawayltd.ews.sample.model.TVehicleCustom;
import com.chinawayltd.ews.sample.model.TVehicleExample;
import com.chinawayltd.ews.sample.model.openApi.TruckDTO;
import com.chinawayltd.ews.sample.service.SynchroVehicleService;
import com.chinawayltd.ews.sample.util.map.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: greeProject.timingTaskCenter
 * @description:
 * @author: xiazj
 * @create: 2018-05-25 13:31
 **/
@Service
public class SynchroVehicleServiceImpl implements SynchroVehicleService {

    private static final String ORGCODE_TRUCK = "201I3D0302";
    private static final String ORGCODE_BUS = "201I3D0301";
    private static final String ORGCODE_LOGISTICS = "201I3D01";

    @Autowired
    private TVehicleMapper tVehicleMapper;
    @Autowired
    private TVehicleCustomMapper tVehicleCustomMapper;
    @Autowired
    private OpenApiManager openApiManager;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    @Transactional
    public void synchroVehicleData() throws Exception{
        List<TruckDTO> trunckList = openApiManager.getTrunckAllByOrgCode(InterfaceConfig.ORG_CODE);
        if(trunckList != null && !trunckList.isEmpty()){
            List<TVehicle> vehicleList = tVehicleMapper.selectByExample(new TVehicleExample());
            Map<String, TVehicle> vehicleMap =  MapUtils.listToMap(vehicleList, "g7sId");
            for (TruckDTO truck : trunckList) {
                String truckId = truck.getTruckid();
                if(StringUtils.isNotBlank(truckId)){
                    TVehicle oldVehicle = vehicleMap.get(truckId);
                    //修改
                    if(oldVehicle != null){
                        updateVehicle(truck, oldVehicle);
                    }else{//新增
                        insertVehicle(truck);
                    }
                }
            }
        }
    }

    /**
     * 更新车辆
     * @param truck
     * @param vehicle
     */
    private void updateVehicle(TruckDTO truck, TVehicle vehicle) throws ParseException {
        String updatetime = truck.getUpdatetime();
        Date updateDate = sdf.parse(updatetime);
        if(vehicle.getUpdateTime().before(updateDate)){
            copyField(truck, vehicle);
            tVehicleMapper.updateByPrimaryKey(vehicle);
        }
    }

    /**
     * 新增车辆
     * @param truck
     */
    private void insertVehicle(TruckDTO truck) throws ParseException {
        //新增车辆信息
    	Date date = new Date();
        TVehicle record = new TVehicle();
        copyField(truck, record);
        record.setCreateTime(record.getUpdateTime());
        tVehicleMapper.insert(record);

        //新增车辆自定义表
        TVehicleCustom custom = new TVehicleCustom();
        custom.setId(record.getId());
        SimpleDateFormat sdf_date_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        LocalDate now = LocalDate.now();
        String beginDate = now.minusDays(7).toString();
        beginDate = beginDate+" 00:00:00";
        Date minusDate = sdf_date_time.parse(beginDate);
        custom.setCarvldTime(minusDate);
        custom.setCreateTime(date);
        custom.setUpdateTime(date);
        tVehicleCustomMapper.insert(custom);
    }

    /**
     * 复制车辆数据
     * @param truck
     * @param vehicle
     */
    private void copyField(TruckDTO truck, TVehicle vehicle) throws ParseException {
        String orgCode = truck.getOrgCode();
        vehicle.setG7sId(truck.getTruckid());
        vehicle.setOwnId(truck.getOwnId());
        vehicle.setCarNum(truck.getCarNum());
        vehicle.setGpsno(truck.getGpsno());
        vehicle.setOrgCode(orgCode);
        vehicle.setFromOrgCode(truck.getFromOrgCode());
        vehicle.setMainDriverId(truck.getMainDriverId());
        vehicle.setDepDriverId(truck.getDepDriverId());
        vehicle.setFromType(truck.getFromType());
        vehicle.setDeleted(truck.getDeleted());

        Byte carType = null;
        if(ORGCODE_TRUCK.equals(orgCode)){
            carType = 0;
        }else if(ORGCODE_BUS.equals(orgCode)){
            carType = 1;
        }else if(ORGCODE_LOGISTICS.equals(orgCode)){
            carType = 2;
        }
        vehicle.setCarType(carType);
        if(StringUtils.isNotBlank(truck.getUpdatetime())){
            vehicle.setUpdateTime(sdf.parse(truck.getUpdatetime()));
        }
    }

}
